package com.mayn.jst.task;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mayn.jst.erp.action.FinanceAction;
import com.mayn.jst.login.TaobaoLoginAction;
import com.mayn.jst.shop.action.ShopAccountAction;
import com.mayn.jst.shop.action.ShopAction;
import com.mayn.jst.shop.model.ShopAccountModel;
import com.mayn.jst.taobao.TaobaoDsrAction;
import com.mayn.util.DateUtil;

/**
 * 店铺数据采集
 * 
 * @author Administrator
 *
 */
public class GetAllShopDataTask {

	private static final Logger LOG = LoggerFactory.getLogger(GetAllShopDataTask.class);

	public static void getAll() {
		Date currTime = new Date();
		getAll(currTime);
	}
	
	public static void getAll(Date currTime) {

		LOG.info("店铺DSR数据采集开始--");
		TaobaoDsrAction.getAllShopDsrData();
		LOG.info("店铺DSR数据采集结束--");

		LOG.info("店铺直通车费用采集开始--");
		getAllSubWayFee(currTime);
		LOG.info("店铺直通车费用采集结束--");

		LOG.info("淘宝钻石展位费用采集开始--");
		getAllZuanshiFee(currTime);
		LOG.info("淘宝钻石展位费用采集结束--");

		LOG.info("淘宝品销宝费用采集开始");
		getAllBrandingFee(currTime);
		LOG.info("淘宝品销宝费用采集结束--");
	}

	public static void getAllSubWayFee() {
		Date currTime = new Date();
		getAllSubWayFee(currTime);
	}

	public static void getAllSubWayFee(Date currTime) {
		int fType = 201;
		try {
			List<ShopAccountModel> shopAccountList = ShopAccountAction.getShopAccountList();
			for (int i = 0; i < shopAccountList.size(); i++) {
				int shopId = shopAccountList.get(i).getInt("SA_SHOP_ID");
				int shopType = ShopAction.getShopType(shopId);
				if (shopType == 1) {
					int year = Integer.parseInt(DateUtil.dateFmtToString(currTime, "yyyy"));
					int month = Integer.parseInt(DateUtil.dateFmtToString(currTime, "MM"));
					int day = Integer.parseInt(DateUtil.dateFmtToString(currTime, "dd"));
					String startDate = "";
					String endDate = "";
					if(day==1){
						startDate = DateUtil.getFirstDayOfMonth(year, month - 1);// 上月第一天
					}else{
						startDate = DateUtil.getFirstDayOfMonth(year, month);// 本月第一天
					}
					endDate = DateUtil.dateFmtToString(DateUtil.getDayStartTime(new Date(currTime.getTime() - 1000 * 60 * 60 * 24)));// 昨天
					LOG.info("开始日期:" + startDate);
					LOG.info("结束日期" + endDate);
					List<Date> dateList = DateUtil.dateSplit(DateUtil.parseDateTime(startDate), DateUtil.parseDateTime(endDate));
					for (int j = 0; j < dateList.size(); j++) {
						String date = DateUtil.dateFmtToString(dateList.get(j), "yyyy-MM-dd");
						if (!FinanceAction.checkfinanceGeneralledgerstatis(shopId, fType, date)) {
							Double fee = TaobaoLoginAction.getSubWayFeeValue(date, shopId);
							//实际扣款日期
							String cutDate = date;
							FinanceAction.saveFeeData(shopId, date,cutDate, fType, fee);
						} else {
							LOG.info("日期" + date + "直通车费用已采集");
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void getAllZuanshiFee() {
		Date currTime = new Date();
		getAllZuanshiFee(currTime);
	}
	public static void getAllZuanshiFee(Date currTime) {
		int fType = 202;
		try {
			List<ShopAccountModel> shopAccountList = ShopAccountAction.getShopAccountList();
			for (int i = 0; i < shopAccountList.size(); i++) {
				int shopId = shopAccountList.get(i).getInt("SA_SHOP_ID");
				int shopType = ShopAction.getShopType(shopId);
				if (shopType == 1) {
					int year = Integer.parseInt(DateUtil.dateFmtToString(currTime, "yyyy"));
					int month = Integer.parseInt(DateUtil.dateFmtToString(currTime, "MM"));
					int day = Integer.parseInt(DateUtil.dateFmtToString(currTime, "dd"));
					String startDate = "";
					String endDate = "";
					if(day==1){
						startDate = DateUtil.getFirstDayOfMonth(year, month - 1);// 上月第一天
					}else{
						startDate = DateUtil.getFirstDayOfMonth(year, month);// 本月第一天
					}
					endDate = DateUtil.dateFmtToString(DateUtil.getDayStartTime(new Date(currTime.getTime() - 1000 * 60 * 60 * 24)));// 昨天
					LOG.info("开始日期:" + startDate);
					LOG.info("结束日期" + endDate);
					List<Date> dateList = DateUtil.dateSplit(DateUtil.parseDateTime(startDate), DateUtil.parseDateTime(endDate));
					for (int j = 0; j < dateList.size(); j++) {
						String date = DateUtil.dateFmtToString(dateList.get(j), "yyyy-MM-dd");
						//实际扣款日期
						String cutDate = DateUtil.dateFmtToString(DateUtil.addDays(DateUtil.stringFmtToDate(date, "yyyy-MM-dd"), 1));
						if (!FinanceAction.checkfinanceGeneralledgerstatis(shopId, fType, date)) {
							Double fee = TaobaoLoginAction.getZuanshiFeeValue(date, shopId);
							FinanceAction.saveFeeData(shopId, date,cutDate, fType, fee);
						} else {
							LOG.info("日期" + date + "钻石展位费用已采集");
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void getAllBrandingFee() {
		Date currTime = new Date();
		getAllBrandingFee(currTime);
	}
	public static void getAllBrandingFee(Date currTime) {
		int fType = 203;
		try {
			List<ShopAccountModel> shopAccountList = ShopAccountAction.getShopAccountList();
			for (int i = 0; i < shopAccountList.size(); i++) {
				int shopId = shopAccountList.get(i).getInt("SA_SHOP_ID");
				int shopType = ShopAction.getShopType(shopId);
				if (shopType == 1) {
					int year = Integer.parseInt(DateUtil.dateFmtToString(currTime, "yyyy"));
					int month = Integer.parseInt(DateUtil.dateFmtToString(currTime, "MM"));
					int day = Integer.parseInt(DateUtil.dateFmtToString(currTime, "dd"));
					String startDate = "";
					String endDate = "";
					if(day==1){
						startDate = DateUtil.getFirstDayOfMonth(year, month - 1);// 上月第一天
					}else{
						startDate = DateUtil.getFirstDayOfMonth(year, month);// 本月第一天
					}
					endDate = DateUtil.dateFmtToString(DateUtil.getDayStartTime(new Date(currTime.getTime() - 1000 * 60 * 60 * 24)));// 昨天
					LOG.info("开始日期:" + startDate);
					LOG.info("结束日期" + endDate);
					List<Date> dateList = DateUtil.dateSplit(DateUtil.parseDateTime(startDate), DateUtil.parseDateTime(endDate));
					for (int j = 0; j < dateList.size(); j++) {
						String date = DateUtil.dateFmtToString(dateList.get(j), "yyyy-MM-dd");
						//实际扣款日期
						String cutDate = DateUtil.dateFmtToString(DateUtil.addDays(DateUtil.stringFmtToDate(date, "yyyy-MM-dd"), 1));
						if (!FinanceAction.checkfinanceGeneralledgerstatis(shopId, fType, date)) {
							Double fee = TaobaoLoginAction.getBrandingFeeValue(date, shopId);
							FinanceAction.saveFeeData(shopId, date,cutDate,fType, fee);
						} else {
							LOG.info("日期" + date + "品销宝费用已采集");
						}
					}
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
